const express = require('express');
const router = express.Router();
const {
	orderItemListModel,
	orderInfoListModel,
	orderRecordListModel,
	submitOrderParamsModel
} = require('../model/order')
const { skuProductListModel } = require('../model/product')
const { leaderAddressModel } = require('../model/pickUpLocation')
const {
	cartInfoListModel,
	activityRuleModel,
	couponInfoListModel,
} = require('../model/cart')


// 确认订单信息
router.get('/auth/confirmOrder', async (req, res) => {
	const data = await orderInfoListModel.findOne()
	const leaderAddressVo = await leaderAddressModel.find()
	const cartInfoList = await cartInfoListModel.find()
	const activityRule = await activityRuleModel.find()
	const couponInfoList = await couponInfoListModel.find()
	const carInfoVoList = [
		{
			activityRule,
			cartInfoList
		}
	]
	res.send({
		code: 200,
		msg: '获取成功',
		data: {
			activityReduceAmount: 2,
			couponReduceAmount: 3,
			originalTotalAmount: 120,
			totalAmount: 115,
			orderNo: '7317155027767590480070453503088132096',
			leaderAddressVo: leaderAddressVo[0],
			couponInfoList,
			carInfoVoList
		}
	})
});

// 提交订单信息
router.post('/auth/submitOrder', async (req, res) => {
	await submitOrderParamsModel.create(req.body)
	const data = await submitOrderParamsModel.find()
	console.log();
	res.send({
		code: 200,
		msg: '提交成功'
	})
});

/*
* 获取指定订单信息
*  orderId -订单信息orderId
* */
router.get('/auth/getOrderInfoById/:orderId', async (req, res) => {
	const data = await submitOrderParamsModel.findOne({ orderNo: req.params.orderId })
	res.send({
		code: 200,
		msg: '获取成功',
		data
	})
});

/*
*  查询用户订单列表
*
* */
router.get('/auth/findUserOrderPage', async (req, res) => {
	const data = await submitOrderParamsModel.find()
	res.send({
		code: 200,
		msg: '获取成功',
		data
	})
});

router.get('/clearCard/:id', async (req, res) => {
	await submitOrderParamsModel.updateOne({ _id: req.params.id }, { status: 1 })
	await cartInfoListModel.deleteMany({});
	res.send({
		code: 200,
		msg: '清除成功'
	})

})

module.exports = router;
